using Microsoft.EntityFrameworkCore;
using NotifyCenter.EntityFrameworkCore;
using NotifyCenter.MediatR;
using NotifyCenter.MediatR.Webhooks;
using Yitter.IdGenerator;
using System.IO;

var builder = WebApplication.CreateBuilder(args);
 
builder.Services.AddControllers(); 
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddHttpClient();

builder.Services.AddFeishuSender();
builder.Services.AddMockSender();

builder.Services.AddDbContext<NotifyCenterDbContext>(opt =>
{
    var connectionString = builder.Configuration.GetConnectionString("NotifyCenter");
    opt.UseNpgsql(connectionString);
});

builder.Services.AddTransient<IProjectService, ProjectService>();
builder.Services.AddMediatR(conf =>
{
    conf.RegisterServicesFromAssemblies(typeof(MediatRLocator).Assembly);
});
builder.Services.AddSingleton<INotificationWebhookQueue, NotificationWebhookQueue>();
builder.Services.AddHostedService<NotificationWebhookDispatcher>();

YitIdHelper.SetIdGenerator(new IdGeneratorOptions(1));

var app = builder.Build();
  
app.UseSwagger();
app.UseSwaggerUI();

app.UseStaticFiles();

app.UseFeishuSender(options =>
{
    options.StartNow = true;
});

app.UseRouting();
app.UseAuthorization();

app.MapControllers();

var adminIndexPath = Path.Combine(app.Environment.WebRootPath ?? Path.Combine(app.Environment.ContentRootPath, "wwwroot"), "admin", "index.html");

app.MapGet("/admin", context =>
{
    context.Response.ContentType = "text/html";
    return context.Response.SendFileAsync(adminIndexPath);
});

app.MapGet("/admin/{**path}", context =>
{
    context.Response.ContentType = "text/html";
    return context.Response.SendFileAsync(adminIndexPath);
});

app.Run();
